//
//  HKThemeManager.m
//  HKiOSTools
//
//  Created by 胡锦涛 on 2020/12/22.
//

#import "HKThemeManager.h"
#import "HKCommonUI.h"
#import "HKUIHelper.h"
@interface HKThemeManager ()

@property(nonatomic, strong) UIColor *hk_backgroundColor;
@property(nonatomic, strong) UIColor *hk_backgroundColorLighten;
@property(nonatomic, strong) UIColor *hk_backgroundColorHighlighted;
@property(nonatomic, strong) UIColor *hk_tintColor;
@property(nonatomic, strong) UIColor *hk_titleTextColor;
@property(nonatomic, strong) UIColor *hk_mainTextColor;
@property(nonatomic, strong) UIColor *hk_descriptionTextColor;
@property(nonatomic, strong) UIColor *hk_placeHolderColor;
@property(nonatomic, strong) UIColor *hk_codeColor;
@property(nonatomic, strong) UIColor *hk_separatorColor;
@property(nonatomic, strong) UIColor *hk_gridItemTintColor;

@property(nonatomic, strong) UIImage *hk_searchBarTextFieldBackgroundImage;
@property(nonatomic, strong) UIImage *hk_searchBarBackgroundImage;

@property(nonatomic, strong) UIVisualEffect *hk_standardBlueEffect;

@property(class, nonatomic, strong, readonly) HKThemeManager *sharedInstance;
@end

@implementation HKThemeManager

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static HKThemeManager *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}

- (instancetype)init {
    if (self = [super init]) {
        self.hk_backgroundColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themeBackgroundColor;
        }];
        self.hk_backgroundColorLighten = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, NSString * _Nullable identifier, NSObject<HKThemeProtocol> * _Nullable theme) {
            return theme.themeBackgroundColorLighten;
        }];
        self.hk_backgroundColorHighlighted = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themeBackgroundColorHighlighted;
        }];
        self.hk_tintColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themeTintColor;
        }];
        self.hk_titleTextColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themeTitleTextColor;
        }];
        self.hk_mainTextColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themeMainTextColor;
        }];
        self.hk_descriptionTextColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themeDescriptionTextColor;
        }];
        self.hk_placeHolderColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themePlaceholderColor;
        }];
        self.hk_codeColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themeCodeColor;
        }];
        self.hk_separatorColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, NSObject<HKThemeProtocol> *theme) {
            return theme.themeSeparatorColor;
        }];
        self.hk_gridItemTintColor = [UIColor qmui_colorWithThemeProvider:^UIColor * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, NSString * _Nullable identifier, NSObject<HKThemeProtocol> * _Nullable theme) {
            return theme.themeGridItemTintColor;
        }];
        
        self.hk_searchBarTextFieldBackgroundImage = [UIImage qmui_imageWithThemeProvider:^UIImage * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, __kindof NSObject<HKThemeProtocol> * _Nullable theme) {
            return [UISearchBar qmui_generateTextFieldBackgroundImageWithColor:theme.themeBackgroundColorHighlighted];
        }];
        self.hk_searchBarBackgroundImage = [UIImage qmui_imageWithThemeProvider:^UIImage * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, __kindof NSObject<NSCopying> * _Nullable identifier, __kindof NSObject<HKThemeProtocol> * _Nullable theme) {
            return [UISearchBar qmui_generateBackgroundImageWithColor:theme.themeBackgroundColor borderColor:nil];
        }];
        
        self.hk_standardBlueEffect = [UIVisualEffect qmui_effectWithThemeProvider:^UIVisualEffect * _Nonnull(__kindof QMUIThemeManager * _Nonnull manager, NSString * _Nullable identifier, NSObject<HKThemeProtocol> * _Nullable theme) {
            return [UIBlurEffect effectWithStyle:[identifier isEqualToString:HKThemeIdentifierDark] ? UIBlurEffectStyleDark : UIBlurEffectStyleLight];
        }];
    }
    return self;
}

+ (NSObject<HKThemeProtocol> *)currentTheme {
    return QMUIThemeManagerCenter.defaultThemeManager.currentTheme;
}

@end

@implementation UIColor (HKTheme)

+ (instancetype)hk_sharedInstance {
    static dispatch_once_t onceToken;
    static UIColor *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

+ (UIColor *)hk_backgroundColor {
    return HKThemeManager.sharedInstance.hk_backgroundColor;
}

+ (UIColor *)hk_backgroundColorLighten {
    return HKThemeManager.sharedInstance.hk_backgroundColorLighten;
}

+ (UIColor *)hk_backgroundColorHighlighted {
    return HKThemeManager.sharedInstance.hk_backgroundColorHighlighted;
}

+ (UIColor *)hk_tintColor {
    return HKThemeManager.sharedInstance.hk_tintColor;
}

+ (UIColor *)hk_titleTextColor {
    return HKThemeManager.sharedInstance.hk_titleTextColor;
}

+ (UIColor *)hk_mainTextColor {
    return HKThemeManager.sharedInstance.hk_mainTextColor;
}

+ (UIColor *)hk_descriptionTextColor {
    return HKThemeManager.sharedInstance.hk_descriptionTextColor;
}

+ (UIColor *)hk_placeHolderColor {
    return HKThemeManager.sharedInstance.hk_placeHolderColor;
}

+ (UIColor *)hk_codeColor {
    return HKThemeManager.sharedInstance.hk_codeColor;
}

+ (UIColor *)hk_separatorColor {
    return HKThemeManager.sharedInstance.hk_separatorColor;
}

+ (UIColor *)hk_gridItemTintColor {
    return HKThemeManager.sharedInstance.hk_gridItemTintColor;
}

@end

@implementation UIImage (HKTheme)

+ (UIImage *)hk_searchBarTextFieldBackgroundImage {
    return HKThemeManager.sharedInstance.hk_searchBarTextFieldBackgroundImage;
}

+ (UIImage *)hk_searchBarBackgroundImage {
    return HKThemeManager.sharedInstance.hk_searchBarBackgroundImage;
}

@end

@implementation UIVisualEffect (HKTheme)

+ (UIVisualEffect *)hk_standardBlurEffect {
    return HKThemeManager.sharedInstance.hk_standardBlueEffect;
}

@end
